#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"

void stackInit(stack* ps) {
	stackData* np = (stackData*)malloc(sizeof(stackData) * MAX);
	if (np == NULL) {
		perror("init->malloc");
	}
	ps->data = np;
	ps->capacity = MAX;
	ps->top = 0;
}

void stackPush(stack* ps, stackData num) {
	assert(ps);
	if (ps->capacity == ps->top) {
		stackData* np = (stackData*)realloc(ps->data,sizeof(stackData) * (ps->capacity+INSERT));
		if (np == NULL) {
			perror("push->malloc");
		}
		ps->data = np;
		ps->capacity += INSERT;
	}
	ps->data[ps->top] = num;
	ps->top++;
}

void StackPop(stack* ps) {
	assert(ps);
	assert(!stackEmpty(ps));
	ps->top--;
}

stackData stackTop(stack* ps) {
	assert(ps);
	assert(!stackEmpty(ps));
	return ps->data[ps->top - 1];
}

int stackSize(stack* ps) {
	assert(ps);
	return ps->top;
}

int stackEmpty(stack* ps) {
	assert(ps);
	return ps->top == 0;
}

void stackDestroy(stack* ps) {
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = 0;
	ps->top = 0;
}